<template>
    <div class="shoppingCart">
      <div class="reload" @click="shuaXin">
        刷新
        <!--<i></i>-->
      </div>
      <div class="empty_box" v-if="!table.length">
        <div class="title_empty sjj">购物车</div>
        <div class="empty sjj">
          <img src="../assets/hello.png" alt="">
          <div class="word">
            <p class="p1">购物车是空的</p>
            <p class="p2">
              请点击下方的“继续点菜”按钮开始点菜
            </p>
          </div>
        </div>
      </div>

      <div class="n_empty_box" v-if="!!table.length">
        <div class="title_n_empty sjj">
          <div class="gwc_title">
            购物车
          </div>
          <div class="change">
            <div class="ycrs red">用餐人数: {{ users_num }}人</div>
            <div class="beizhu">备注: {{ note===''?'无':note}}</div>
            <i class="change_icon" @click="toDinersNum"></i>
          </div>
          <div class="dishes_all">
            购物车里有: {{ caiWord }}
            <i class="delete_icon" @click="clearTable"></i>
          </div>
        </div>
        <div class="menu-detail sjj" v-for="(item,i) in table">
          <h3 class="cailei">{{ item.type_name }}</h3>
          <ul>
            <li class="every_cai" v-for="(lis,j) in item.list">
              <div class="user_info">
                <img :src="lis.user_pic" alt="" class="user_pic" >
                <p class="user_name">{{ lis.user_name }}</p>
              </div>
              <div class="cai_info">
                <p class="cai_name">{{ lis.name }}</p>
                <p class="cai_price red">￥{{ lis.price/100 }}</p>
              </div>
              <div class="minus_add_box">
                <div class="add_btn fl" :data-i="i+''+j" @click="minus_count(lis.recipe_id,lis.name,$event)">

                </div>
                <div class="count fl">{{ lis.order_num }}</div>
                <div class="minus_btn fl" :data-i="i+''+j" @click="add_count(lis.recipe_id,lis.d_num,$event)">

                </div>
              </div>
            </li>


          </ul>
        </div>

      </div>
      <div class="bottom_btns">
        <div class="b_btn btn_nav" @click="showNav">
          <i></i>
          导航
        </div>
        <router-link :to="{path: '/dishesList'}" >
          <div class="b_btn btn_jxdc">
            <i></i>
            继续点菜
          </div>
        </router-link>


          <div class="b_btn btn_order" @click="clickXiadan">
            <i></i>
            下单
          </div>

      </div>

      <div class="mask">

      </div>
      <div class="bugou">特色大鸡爪不够了!</div>
      <div class="shanchu ">
        <div class="ct">不想要“特色大鸡爪”了吗？</div>
        <div class="btns">
          <span class="cancel" @click="cancel">取消</span>
          <span class="sure" @click="sure">不要了</span>
        </div>
      </div>

      <div class="nav_detail">
        <ul>
          <li class="dpxx" @click="godpxx">
            <span>店铺信息</span>
          </li>
          <li class="cd" @click="gocd">
            <span>菜单</span>
          </li>
          <li class="gwc" @click="hideNav">
            <span>购物车</span>
          </li>
          <li class="ydgdc" @click="goydgdc">
            <span>已下单的菜</span>
          </li>
          <li class="jz" @click="gojz">
            <span>结账</span>
          </li>
          <li class="fh" @click="hideNav">
            <span>返回</span>
          </li>
        </ul>
      </div>

      <div class="bg"></div>
    </div>
</template>

<script>
    export default {
      data (){
        return {
          table:'',
          caiWord:'',
          arr_count:'',
          now_recipe_id:'',
          note:'',
          users_num:''

        }
      },
      created (){
        console.log('created')
        /*获取购物车信息*/
        this.$http.get(this.kdUrl.url_host+'version=1&resource=table&method=table&token=' + localStorage.getItem("kd_token")).then((res) => {
          console.log('查看购物车11');
          console.log(res);
          /*人数和备注*/
          this.note = res.body.data.note
          this.users_num = res.body.data.users_num
          let caiWord = ''
          for(let i=0;i<res.body.data.table.length;i++){
            this.arr_count

            if(i == res.body.data.table.length-1){
              caiWord += res.body.data.table[i].type_name+res.body.data.table[i].list.length+'个'
            }else{
              caiWord += res.body.data.table[i].type_name+res.body.data.table[i].list.length+'个，'
            }

          }
          this.caiWord = caiWord
          this.table = res.body.data.table
        },(err) => {
          console.log(err);
        })
      },
      mounted (){

      },
      methods:{
        add_count (recipe_id,d_num,e){
          let n = e.target.previousElementSibling.innerHTML
          if(n == d_num){
            n = d_num+1
            e.target.previousElementSibling.innerHTML = d_num
          }else{
            n++
            e.target.previousElementSibling.innerHTML = n
          }

          this.$http.get(this.kdUrl.url_host+'version=1&resource=table&method=change_order&token=' + localStorage.getItem("kd_token")+'&recipe_id='+recipe_id+'&order_num='+n).then((res) => {
            if(res.body.error_code){
              $('.bugou').html(res.body.error_message)
              $('.mask').fadeIn()
              $('.bugou').fadeIn()
              setTimeout(function () {
                $('.mask').fadeOut()
                $('.bugou').fadeOut()
                $('.bugou').html('')
              },2000)
            }
            console.log(res);

            },(err) => {
              console.log(err);
            })
        },
        minus_count (recipe_id,cai_name,e){
          console.log(e)
          let n = e.target.nextElementSibling.innerHTML
          if(n == 1){
            n = 0
            $('.shanchu .ct').html('不想要“'+cai_name+'”了吗？')
            $('.mask').fadeIn()
            $('.shanchu').fadeIn()
            this.now_recipe_id = recipe_id

          }else{
            n--
            e.target.nextElementSibling.innerHTML = n
            this.$http.get(this.kdUrl.url_host+'version=1&resource=table&method=change_order&token=' + localStorage.getItem("kd_token")+'&recipe_id='+recipe_id+'&order_num='+n).then((res) => {
              console.log("减小")
              console.log(res);
            },(err) => {
              console.log(err);
            })
          }


        },
        test (){
          console.log('测试')
        },
        cancel (){
          $('.mask').fadeOut()
          $('.shanchu').fadeOut()
        },
        sure (){
          $('.mask').fadeOut()
          $('.shanchu').fadeOut()
          this.$http.get(this.kdUrl.url_host+'version=1&resource=table&method=change_order&token=' + localStorage.getItem("kd_token")+'&recipe_id='+this.now_recipe_id+'&order_num='+0).then((res) => {
            console.log("删了一个菜")
            console.log(res);
            this.$router.go(0)
          },(err) => {
            console.log(err);
          })

        },
        clearTable (){
          this.$http.get(this.kdUrl.url_host+'version=1&resource=table&method=clear_table&token=' + localStorage.getItem("kd_token")).then((res) => {
            console.log('清空购物车')
            this.$router.go(0)
          },(err) => {
            console.log(err);
          })
        },
        shuaXin (){
          this.$router.go(0)
        },
        showNav (){
          $('.mask').fadeIn()
          $('.nav_detail').fadeIn()
        },
        hideNav (){
          $('.mask').fadeOut()
          $('.nav_detail').fadeOut()
        },
        godpxx (){
          this.$router.push('/restaurantInfo')
        },
        gocd (){
          this.$router.push('/dishesList')
        },
        gogwc (){
          this.$router.push('/shoppingCart')
        },
        goydgdc (){
          this.$router.push('/order')
        },
        gojz (){
          this.$router.push('/order')
        },
        toDinersNum (){
          this.$router.push({path:'/dinersNum',query:{flag:2,users_num:this.users_num,note:this.note}})
        },
        clickXiadan (){
          this.$http.get(this.kdUrl.url_host+'version=1&resource=table&method=confirm_order&token=' + localStorage.getItem("kd_token")).then((res) => {
            console.log('点击下单');
            console.log(res)
            this.$router.push({path:'/order'})
          },(err) => {

          })
        }
      }
    }
</script>

<style scoped>
  .shoppingCart{
    padding: 10px 2.5% 70px;
    width: 100%;
  }
  .reload{
    position: fixed;
    z-index: 10;
    top: 15px;;
    right: 15px;
    width: 30px;
    height: 30px;
    background-image: url('../assets/theme_dark.png');
    background-repeat: no-repeat;
    background-size: 333px 400px;
    background-position: -5px -5px;
    color: #fff;
    line-height: 30px;
    text-align: center;
  }

  .title_empty,.gwc_title{
    height: 40px;
    line-height: 40px;
    text-align: center;
    font-size: 17px;
    position: relative;
    background: #fff;
  }
  .sjj{
    position: relative;
  }
  .sjj:before{
    content: '';
    position: absolute;
    display: block;
    height: 8px;
    width: 100%;
    top: -3px;;
    left: 0;
    background: url('../assets/dian1.png');
  }
  .sjj:after{
    content: '';
    position: absolute;
    display: block;
    height: 8px;
    width: 100%;
    bottom: -3px;;
    left: 0;
    background: url('../assets/dian1.png');
  }

  .empty{
    margin-top: 20px;
    height: 130px;
    padding: 15px 2% 10px 10%;
    background: #fff;
  }
  .empty img{

    width: 25%;
    vertical-align: middle;
  }
  .word{
    display: inline-block;
    vertical-align: middle;
    padding-left: 10%;
    width: 73%;
  }
  .word .p1{
    font-size: 18px;
    margin-bottom: 10px;
    width: 100%;
  }
  .word .p2{
    width: 100%;
    font-size: 14px;
    color: #999;
  }
  .n_empty_box,.empty_box{
    /*display: none;*/
  }
  .title_n_empty{
    background: #fff;
    margin-bottom: 20px;
    padding: 0 10px 7px;
  }
  .change{
    height: 50px;
    padding: 5px 0;
    border-top: 1px solid #ddd;
    border-bottom: 1px solid #ddd;
    position: relative;
  }
  .ycrs{
    font-size: 14px;
    margin-bottom: 2px;
  }
  .beizhu{
    font-size: 12px;
  }
  .red{
    color: #EC313D;
  }
  .dishes_all{
    position: relative;
    min-height: 40px;
    padding-right: 50px;
    padding-top: 10px;
    color: #307ad4;
    font-size: 12px;
    line-height: 20px;
  }
  .change_icon{
    width: 20px;
    height: 30px;
    background: url('../assets/change.png');
    background-size: 20px 30px;
    position: absolute;
    right: 0;
    top: 8px;
  }
  .delete_icon{
    width: 22px;
    height: 32px;
    background: url('../assets/delete.png');
    background-size: 22px 32px;
    position: absolute;
    right: 0;
    top: 10px;
  }



  .menu-detail{
    margin-bottom: 20px;
    background: #fff;
    padding: 0 10px;
  }
  .cailei{
    color: #307ad4;
    font-size: 17px;
    height: 40px;
    line-height: 40px;
    text-align: center;

  }
  .every_cai{
    height: 80px;
    border-top: 1px solid #ddd;
    padding-top: 10px;
    position: relative;
  }
  .user_info,.cai_info,.minus_add_box{
    display: inline-block;
    vertical-align: middle;
  }

  .user_info img{
    width: 40px;
    height: 40px;
    border-radius: 50%;
  }
  .user_name{
    text-align: center;
  }
  .cai_info{
    margin-left: 15px;
    width: 40%;
  }
  .cai_name{
    width: 100%;
    font-size: 16px;
    margin-bottom: 5px;
    overflow: hidden;
    text-overflow:ellipsis;
    white-space: nowrap;
  }
  .count{
    padding: 0 15px;
    line-height: 30px;
  }
  .add_btn,.minus_btn{
    width: 30px;
    height: 30px;
    border: 1px solid #ddd;
  }
  .minus_add_box{
    position: absolute;
    right: 0;
    top:22px;
  }
  .add_btn{
    background-image: url('../assets/add_minus.png');
    background-repeat: no-repeat;
    background-size:41px 13px;
    background-position: -20px  14px;
  }
  .minus_btn{
    background-image: url('../assets/add_minus.png');
    background-repeat: no-repeat;
    background-size:41px 13px;
    background-position:  8px 8px;
  }
  .bottom_btns{
    height: 60px;
    width: 100%;
    position: fixed;
    bottom: 20px;
    left: 0;
  }
  .b_btn{
    position: absolute;
    top:0;
    width: 60px;
    height: 60px;
    border-radius: 50%;
    border:2px solid #fff;
    text-align: center;
    padding-top: 28px;
    background: #313131;
    color: #fff;
    font-size: 12px;
  }

  .btn_order{
    background: #ce2101;
    right: 2.5%;

  }
  .btn_jxdc{
    left: 50%;
    transform: translateX(-50%);
  }

  .btn_nav{
    left: 2.5%;
  }
  .btn_jxdc i,.btn_order i,.btn_nav i{
    position: absolute;
    width: 30px;
    height: 25px;
    background-image: url('../assets/theme_dark.png');
    background-repeat: no-repeat;
    background-size: 600px 720px;
  }
  .btn_jxdc i{
    top: 6px;
    left: 13px;
    background-position: -416px -372px;
  }
  .btn_order i{
    top: 7px;
    left: 15px;
    background-position: -227px -374px;
  }
  .btn_nav i{
    top: 5px;
    left: 15px;
    background-position: -129px -276px;
  }
  /*弹出框*/
  .mask{
    position: fixed;
    width: 100%;
    height: 100%;
    top:0;
    left: 0;
    background: #000;
    opacity: 0.6;
    z-index: 10;
    display: none;

  }
  .bugou{
    height: 35px;
    text-align: center;
    line-height: 35px;
  }
  .shanchu,.bugou{
    display: none;
    position: fixed;
    border-radius: 5px;
    font-size: 13px;
    width: 80%;
    background: #fff;
    top:35%;
    left: 50%;
    transform: translateX(-50%);
    z-index: 20;
  }
  .shanchu .ct{
    padding: 15px 0;
    text-align: center;
    border-bottom: 1px solid #ddd;
   }
  .btns{
    height: 40px;
    overflow: hidden;
  }
  .btns .cancel,.btns .sure{
    float: left;
    height: 40px;
    width: 50%;
    line-height: 40px;
    text-align: center;
    color: #307ad4;
  }
  .btns .cancel{
    border-right: 1px solid #ddd;

  }

  .nav_detail{
    width: 100%;
    height: 100%;
    position: fixed;
    top: 0;
    left: 0;
    z-index: 20;
    display: none;
  }
  .nav_detail ul{
    position: absolute;
    left: 2.5%;
    bottom: 20px;
  }
  .nav_detail li{
    width: 60px;
    height: 60px;
    border:2px solid #fff;
    background: #313131;
    margin-bottom: 10px;
    border-radius: 50%;
    position: relative;
  }
  .nav_detail li:last-of-type{
    margin-bottom: 0;
  }
  .nav_detail li:before{
    content: '';
    position: absolute;
    top: 17px;;
    left: 15px;
    width: 30px;
    height: 25px;
    background-image: url('../assets/theme_dark.png');
    background-repeat: no-repeat;
    background-size: 600px 720px;
  }
  .nav_detail li span{
    color: #fff;
    width: 90px;
    position: absolute;
    top: 17px;
    font-size: 14px;
    left: 72px;
    text-align: left;
  }
  .nav_detail li.dpxx:before{
    background-position: -516px -563px;
    top: 14px;;
    left: 16px;
  }
  .nav_detail li.cd:before{
    background-position: -416px -372px;
    top: 15px;;
    left: 12px;
  }
  .nav_detail li.gwc:before{
    background-position: -322px -372px;
    top: 14px;;
    left: 14px;
  }
  .nav_detail li.ydgdc:before{
    background-position: -228px -562px;
    top: 14px;;
    left: 16px;
  }
  .nav_detail li.jz:before{
    background-position: -130px -562px;
    top: 12px;;
    left: 15px;
  }
  .nav_detail li.fh:before{
    background-position: -130px -471px;
  }
  .bg {
    background-image: url('../assets/bg.jpg');
    -webkit-background-size: cover;
    background-size: cover;
    position: fixed;
    z-index: -1;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
  }
</style>
